package com.smartleanx.module.hrm.controller.admin.examine.examineFlowPath;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineFlowPath.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineFlowPath.ExamineFlowPathDO;
import com.smartleanx.module.hrm.service.examine.examineFlowPath.ExamineFlowPathService;

@Tag(name = "管理后台 - 考核流程")
@RestController
@RequestMapping("/hrm/examine-flow-path")
@Validated
public class ExamineFlowPathController {

    @Resource
    private ExamineFlowPathService examineFlowPathService;

    @PostMapping("/create")
    @Operation(summary = "创建考核流程")
    @PreAuthorize("@ss.hasPermission('hrm:examine-flow-path:create')")
    public CommonResult<Integer> createExamineFlowPath(@Valid @RequestBody ExamineFlowPathSaveReqVO createReqVO) {
        return success(examineFlowPathService.createExamineFlowPath(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核流程")
    @PreAuthorize("@ss.hasPermission('hrm:examine-flow-path:update')")
    public CommonResult<Boolean> updateExamineFlowPath(@Valid @RequestBody ExamineFlowPathSaveReqVO updateReqVO) {
        examineFlowPathService.updateExamineFlowPath(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-flow-path:delete')")
    public CommonResult<Boolean> deleteExamineFlowPath(@RequestParam("id") Integer id) {
        examineFlowPathService.deleteExamineFlowPath(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核流程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-flow-path:query')")
    public CommonResult<ExamineFlowPathRespVO> getExamineFlowPath(@RequestParam("id") Integer id) {
        ExamineFlowPathDO examineFlowPath = examineFlowPathService.getExamineFlowPath(id);
        return success(BeanUtils.toBean(examineFlowPath, ExamineFlowPathRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核流程分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-flow-path:query')")
    public CommonResult<PageResult<ExamineFlowPathRespVO>> getExamineFlowPathPage(@Valid ExamineFlowPathPageReqVO pageReqVO) {
        PageResult<ExamineFlowPathDO> pageResult = examineFlowPathService.getExamineFlowPathPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineFlowPathRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核流程 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-flow-path:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineFlowPathExcel(@Valid ExamineFlowPathPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineFlowPathDO> list = examineFlowPathService.getExamineFlowPathPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核流程.xlsx", "数据", ExamineFlowPathRespVO.class,
                        BeanUtils.toBean(list, ExamineFlowPathRespVO.class));
    }

}